package main

import (
	"context"
	"fmt"
	"log"
	"net"

	"gitee.com/go-course/go12/skills/grpc/hello"
	"google.golang.org/grpc"
)

func main() {
	// 首先是通过grpc.NewServer()构造一个gRPC服务对象
	grpcServer := grpc.NewServer()

	// net/rpc --> grpc
	// 1. 注册 需要暴露的rpc对象给 grpc框架
	hello.RegisterHelloServiceServer(grpcServer, &HelloServiceImpl{})

	lis, err := net.Listen("tcp", ":1234")
	if err != nil {
		log.Fatal(err)
	}

	// 2.然后通过grpcServer.Serve(lis)在一个监听端口上提供gRPC服务
	grpcServer.Serve(lis)
}

// HelloService的服务端实现 Controller
type HelloServiceImpl struct {
	// 继承grpc标准实现对象
	// 继承 + 重写(Overwrite)
	hello.UnimplementedHelloServiceServer
}

//	type HelloServiceServer interface {
//		// Hello(HelloRequest, HelloResponse)
//		Hello(context.Context, *HelloRequest) (*HelloResponse, error)
//		mustEmbedUnimplementedHelloServiceServer()
//	}
func (i *HelloServiceImpl) Hello(
	ctx context.Context,
	in *hello.HelloRequest) (
	*hello.HelloResponse,
	error) {
	resp := &hello.HelloResponse{}
	resp.Message = fmt.Sprintf("hello, %s", in.Name)
	return resp, nil
}
